<?php
/*****************************************************************************
 *                                                                            *
 * Copyright (c) 1999-2007 Wimba, All Rights Reserved.                        *
 *                                                                            *
 * COPYRIGHT:                                                                 *
 *      This software is the property of Wimba.                               *
 *      You can redistribute it and/or modify it under the terms of           *
 *      the GNU General Public License as published by the                    *
 *      Free Software Foundation.                                             *
 *                                                                            *
 * WARRANTIES:                                                                *
 *      This software is distributed in the hope that it will be useful,      *
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *      GNU General Public License for more details.                          *
 *                                                                            *
 *      You should have received a copy of the GNU General Public License     *
 *      along with the Wimba Moodle Integration;                              *
 *      if not, write to the Free Software Foundation, Inc.,                  *
 *      51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                *
 *                                                                            *
 * Author: Frederic Mathiot                                                   *
 *                                                                            *
 * Date: 16 november 2007                                                     *
 *                                                                            *
 ******************************************************************************/

require_once('../../config.php');
require_once('./prontolib.php');

require_login();

if (!is_siteadmin()) {
   print_error(get_string('accessdenied','admin'));
}

$PAGE->set_context(get_system_context());
$PAGE->set_url('/mod/pronto/logs.php');
$PAGE->set_heading(get_string('serverlogs', 'pronto'));
$PAGE->set_pagelayout('admin');
echo $OUTPUT->header();
echo $OUTPUT->box_start();

echo "<a href='javascript:history.back()'><span>" . get_string('logback' , 'pronto') . "</span></a>";

$logTable = new html_table();
$logTable->tablealign = 'center';
$header = new html_table_cell(get_string('serverlogs', 'pronto'));
$header->attributes['class'] = 'header';
$header->colspan = 3;
$headerRow = new html_table_row(array($header));
$tableRows = array();
$tableRows[] = $headerRow;

array_multisort(array_map('filemtime', ($files = glob(PRONTO_LOGS_DIR . "/*.log"))), SORT_DESC, $files);

foreach ($files as $filename) {
   $nameCell = new html_table_cell(getLogDownloadLink(basename($filename)));
   $dateCell = new html_table_cell(userdate(filemtime($filename)));
   $sizeCell = new html_table_cell(getLogSizeInKB(filesize($filename)));
   $row = new html_table_row(array($nameCell, $dateCell, $sizeCell));
   $tableRows[]  = $row;
}
if (! sizeof($files)){
   $tableRows[] = new html_table_row(array(get_string('no_logs' , 'pronto')));
}

$logTable->data =  $tableRows;
echo html_writer::table($logTable);

echo $OUTPUT->box_end();
echo $OUTPUT->footer();

function getLogSizeInKB($byteSize) {
   return floor($byteSize / 1024) . " KB";
}

function getLogDownloadLink($logName) {
   $strippedName = basename($logName, ".log");
   $logUrl = "download-log.php?logname=" . $strippedName;
   $logLink = "<a href='" . $logUrl . "'>" . $logName . "</a>";
   return $logLink;
}

